﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Temp.Controller;
using Temp.Model;

namespace Temp.NLG
{
    class ItemDescriber
    {
        private List<string> sentenceList;
        private StringBuilder paragraph;
        private List<ItemController> listAttributes;

        private MessageTempController mtc;
        private ObjectController oc;
        private ItemController ic;
        private LexiconController lc;

        public ItemDescriber()
        {
            paragraph = new StringBuilder();
            mtc = new MessageTempController();
            oc = new ObjectController();
            ic = new ItemController();
            lc = new LexiconController();
        }

        public List<Conversation> getItemDescription(string itemName)
        {
            List<Conversation> listConversation = new List<Conversation>();
            paragraph.Clear();
            if (sentenceList != null)
            {
                sentenceList.Clear();
                System.Diagnostics.Debug.WriteLine("sentence list cleared " + sentenceList.Count);
            }

            if (listAttributes != null)
            {
                listAttributes.Clear();
                System.Diagnostics.Debug.WriteLine("attrib list cleared " + listAttributes.Count);
            }

            sentenceList = mtc.getMessageTemplateForItems();

            System.Diagnostics.Debug.WriteLine("SENTENCE COUNT" + sentenceList.Count + "\n");
            foreach (string s in sentenceList)
            {
                System.Diagnostics.Debug.WriteLine("SENTENCE" + s + "\n");
                listConversation.Add(fillUpSentence(s, itemName));
            }
            System.Diagnostics.Debug.WriteLine("THIS IS PARAGRAPH " + paragraph.ToString());
            return reindexConversationList(listConversation);

        }

        private List<Conversation> reindexConversationList(List<Conversation> listConversation)
        {
            List<Conversation> tempConversationList = new List<Conversation>();
            tempConversationList.Add(listConversation[0].clone());
            for (int i = 1; i < listConversation.Count; i++)
            {
                if (listConversation[i].optionList.Count == 0)
                {
                    tempConversationList[0].dialogue += " " + listConversation[i].dialogue;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("trigger name : " + listConversation[i].nameOfTriggerer + "trigger type: " + listConversation[i].typeOfTriggerer);
                    tempConversationList.Add(listConversation[i].clone());
                    System.Diagnostics.Debug.WriteLine("trigger name : " + tempConversationList.Last().nameOfTriggerer + "trigger type: " + tempConversationList.Last().typeOfTriggerer);

                }
            }
            return tempConversationList;
        }

        private Conversation fillUpSentence(string s, string itemName)
        {
            Conversation c = new Conversation("Alex", "", 'T');
            MatchCollection matchC;
            String pattern = "<([a-z]|[A-Z]| )+>";

            matchC = Regex.Matches(s, pattern);

            foreach (Match m in matchC)
            {
                System.Diagnostics.Debug.WriteLine("MATCH VALUE:" + m.Value);
                s = s.Replace(m.Value.ToString(), getRelationValue(m.Value, c, itemName));
            }
            c.dialogue = s;
            return c;
        }

        private string getRelationValue(string matchVal, Conversation c, string itemName)
        {
            string temp = matchVal;
            temp = temp.Replace("<", "");
            temp = temp.Replace(">", "");
            String l = lc.getSynonym(temp, "Lexicon_Relation");
            List<Ontology> listItemOnt = ic.getAttributeValue(temp, itemName);
            System.Diagnostics.Debug.WriteLine("list item ont count" + listItemOnt.Count + " name " + itemName);
            if (listItemOnt.Count > 1)
            {
                System.Diagnostics.Debug.WriteLine("list item count > 2");
                foreach (Ontology io in listItemOnt)
                {
                    c.optionList.Add(io.concept2);
                }
                c.nameOfTriggerer = itemName;
                c.typeOfTriggerer = "Item";
                System.Diagnostics.Debug.WriteLine("IN ITEM DESC ITEM = " + itemName);
                return "";
            }

            else if (listItemOnt.Count == 1)
            {
                System.Diagnostics.Debug.WriteLine("list item count < 1 value:" + listItemOnt[0].concept2);

                if (l != null)
                    return l + " " + listItemOnt[0].concept2;
                else
                    return listItemOnt[0].concept2;

            }

            return itemName;
        }
    }
}
